package com.jz.banner.screen.banner.utils

import com.blankj.utilcode.util.NetworkUtils
import com.jz.banner.screen.banner.bean.DeviceInfoBean
import com.jz.banner.screen.banner.bean.WiFiBean
import com.jz.banner.screen.ui.App
import com.jz.banner.screen.utils.*

object DeviceInfoUtils {
    var deviceInfoBean: DeviceInfoBean = DeviceInfoBean()
    var wifiBean: WiFiBean = WiFiPunchHelper.getWiFiBean(App.instance())
    var mMd5DeviceInfo: String? = null
    var mDeviceInfoJson: String? = null


    //抓取本地设备信息，一般放在用于变动数据地方调用。
    fun fetchDeviceInfoBean(): DeviceInfoBean {
        //用户信息
        deviceInfoBean.login_user_name = SPUtils.getString(Params.contact_name).isNotAvailable()
        deviceInfoBean.login_user_phone = SPUtils.getString(Params.contact_mobile).isNotAvailable()
        deviceInfoBean.market_name = SPUtils.getString(Params.mar_name).isNotAvailable()
        deviceInfoBean.merchant_name = SPUtils.getString(Params.shop_name).isNotAvailable()
        deviceInfoBean.stall_name = SPUtils.getString(Params.stall_name).isNotAvailable()
        deviceInfoBean.shop_cat_name = SPUtils.getString(Params.shop_cat_name).isNotAvailable()

        //软件信息
        deviceInfoBean.device_sn = UniqueIDUtils.getUniqueID()
        deviceInfoBean.package_name = AppUtils.getPackageName()
        deviceInfoBean.app_name = AppUtils.getAppName()

        //经纬度
        deviceInfoBean.softVersion_name = AppUtils.getVersionName()
        deviceInfoBean.soft_version_code = AppUtils.getVersionCode()

        //设备信息
        deviceInfoBean.device_brand = EquipmentUtil.getDeviceBrand()
        deviceInfoBean.device_sys_model = EquipmentUtil.getSystemModel()
        deviceInfoBean.device_sys_version = EquipmentUtil.getSystemVersion()
        deviceInfoBean.device_name = EquipmentUtil.getDeviceManufacturer()
        deviceInfoBean.device_board = EquipmentUtil.getDeviceBoard()

        //网络信息
        deviceInfoBean.device_local_ip = NetworkUtils.getIPAddress(true)
        deviceInfoBean.device_local_mac = wifiBean.wifiMac
        deviceInfoBean.device_local_gateway = NetworkUtils.getGatewayByWifi()
        deviceInfoBean.device_local_wifi_name = wifiBean.wifiName

        try {
            val locationByGps = GpsUtils.getLocationByGps()?.split(",")
            deviceInfoBean.device_longitude = locationByGps?.get(0)
            deviceInfoBean.device_latitude = locationByGps?.get(1)
        } catch (e: Exception) {
            CommUtils.println("getLocationByGps:", "GPS Error:$e")
        }
        mDeviceInfoJson = CommUtils.gson.toJson(deviceInfoBean)
        mDeviceInfoJson?.let { CommUtils.println("同步上传数据：", it) }
        mDeviceInfoJson?.let { mMd5DeviceInfo = CommUtils.encodeByMD5(it) }

        return deviceInfoBean!!
    }
}